package com.tfswx.ruleengine.database.bean.table;

import lombok.Data;

import java.util.List;

/**
 * @author Ricky
 * @date 2021/7/10
 */
@Data
public class Table {
    private String tableName;
    private String tableComment;
    private List<TabeleField> fieldList;

    public Table(String tableName, String tableComment) {
        this.tableName = tableName;
        this.tableComment = tableComment;
    }

    /**
     * 达梦建表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getDmCreateSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE ").append(tableName).append(" (");
        StringBuilder pk = new StringBuilder();
        fieldList.forEach(item -> {
            if (Boolean.TRUE.equals(item.getZj())) {
                pk.append(item.getZdmc()).append(",");
            }
            sb.append("\n\t").append(item.getDmZdlxStr()).append(",");
        });
        // 主键 PRIMARY KEY('')
        pk.deleteCharAt(pk.length() - 1);
        sb.append("PRIMARY KEY(").append(pk).append(")");
        sb.append("\n").append(");");
        return sb.toString();
    }

    /**
     * Mysql建表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getMysqlCreateSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" (");
        fieldList.forEach(item -> sb.append("\n\t").append(item.getMysqlZdlxStr()).append(","));
        sb.deleteCharAt(sb.length() - 1);
        sb.append("\n").append(") COMMENT '").append(tableComment).append("';");
        return sb.toString();
    }

    /**
     * 达梦表注释语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getDmTableCommentSql() {
        return "COMMENT ON TABLE " + tableName + " IS '" + tableComment + "';";
    }

    /**
     * 达梦删表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getDmDropSql() {
        return "DROP TABLE IF EXISTS " + tableName + ";";
    }

    /**
     * Mysql删表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getMysqlDropSql() {
        return "DROP TABLE IF EXISTS " + tableName + ";";
    }

    /**
     * 达梦清空表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getDmTruncateSql() {
        return "TRUNCATE TABLE " + tableName + ";";
    }

    /**
     * mysql清空表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    public String getMysqlTruncateSql() {
        return "TRUNCATE TABLE " + tableName + ";";
    }
}
